﻿using System;
using Medicalsystem.MCP.FormDesigner.Common;
using Medicalsystem.MCP.FormDesigner.Define;
using Medicalsystem.MCP.FormDesigner.Interface;

namespace Medicalsystem.MCP.FormDesigner.Property
{
    /// <summary>
    /// 控件属性的XML转换
    /// 继承 ICtrlBase 接口的控件使用
    /// </summary>
    public class CtrlBasePropertyXML : IXMLConvertor
    {
        /// <summary>
        /// 属性串的目标对象
        /// </summary>
        protected System.Windows.Forms.Control _control;

        /// <summary>
        /// 控件基本属性的XML转换
        /// </summary>
        /// <param name="C"></param>
        public CtrlBasePropertyXML(System.Windows.Forms.Control C)
        {
            _control = C;
        }

        #region IXMLConvertor 成员

        public virtual string ToXML()
        {
            return string.Format("<ID>{0}</ID>", ((ICtrlBase)_control).CtrlID);//"<ID>" + ((ICtrlBase)_control).CtrlID + "</ID>";
        }

        public void FromXML(string strXML)
        {
            if (strXML.Length > 0)
            {
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.LoadXml(string.Format("<CtrlBase>{0}</CtrlBase>", strXML));

                FromXML(doc.DocumentElement.ChildNodes);
            }
        }

        public virtual void FromXML(System.Xml.XmlNodeList xns)
        {
            if (xns != null && xns.Count > 0)
            {
                foreach (System.Xml.XmlNode XN in xns)
                {
                    switch (XN.Name.ToLower())
                    {
                        case "id":
                            if (((ICtrlBase)_control).DesignModeState == EDesignModeState.Design)
                            {
                                //设计模式下 对新增控件需要进行 Name 唯一性判定 （如 ： 控件复制粘贴）
                                if (((ICtrlBase)_control).Model.Controls.Ctrls.Find(delegate(CtrlMng cm) { return cm.Ctrl.CtrlID == XN.InnerText.Trim(); }) == null)
                                {
                                    ((ICtrlBase)_control).CtrlID = XN.InnerText.Trim();
                                }
                                else
                                {
                                    ((ICtrlBase)_control).CtrlID = string.Format("{0}_{1}", _control.GetType().Name, System.Guid.NewGuid().ToString());
                                }
                            }
                            else
                            {
                                ((ICtrlBase)_control).CtrlID = XN.InnerText.Trim();
                            }
                            break;
                    }
                }
            }
        }

        #endregion IXMLConvertor 成员

        #region 辅助方法

        /// <summary>
        /// Bool型转换字符型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        protected string BoolToString(bool value)
        {
            if (value)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

        /// <summary>
        /// 字符型转换Bool型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        protected bool StringToBool(string value)
        {
            if (value.Trim().Equals("0"))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 转换对象
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        protected string ObjectToString(Object value)
        {
            if (value == null)
            {
                return "";
            }
            else
            {
                return value.ToString();
            }
        }

        /// <summary>
        /// 字符串转换为图片
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        protected System.Drawing.Image ConvertStringToImage(string str)
        {
            try
            {
                byte[] ImageBytes = Convert.FromBase64String(str);
                System.IO.MemoryStream ImageMem = new System.IO.MemoryStream(ImageBytes);
                return System.Drawing.Image.FromStream(ImageMem);
            }
            catch
            { }

            return null;
        }

        /// <summary>
        /// 图片转换为字符串
        /// </summary>
        /// <param name="IMG"></param>
        /// <returns></returns>
        protected string ConvertImageToString(System.Drawing.Image IMG)
        {
            if (IMG == null) return "";

            try
            {
                System.IO.MemoryStream ImageMem = new System.IO.MemoryStream();
                IMG.Save(ImageMem, IMG.RawFormat);
                byte[] ImageBytes = ImageMem.GetBuffer();
                return Convert.ToBase64String(ImageBytes);
            }
            catch
            { }

            return "";
        }

        #endregion 辅助方法
    }
}